<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    
    public function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model("user_model");
        $this->load->model("reminder_model");
    }
    
	public function index()
	{
        $data = array(
            'title' => '欢迎使用捷立信档案管理系统'
        );
        
        $reminders = $this->reminder_model->fetch_reminders();
        $data['reminders'] = $reminders;
        
		$this->load->view('header', $data);
        $this->load->view('reminders', $data);
        $this->load->view('footer');
	}
    
    /**
     * 登录
     */
    public function login() {
        $data = array(
            'title' => '捷立信档案管理系统 - 登录'
        );
        if ($this->session->flashdata('relogin')) {
            $data['error_message'] = $this->session->flashdata('relogin');
        }

        if (count($_POST) > 0) {
            /* 有提交数据 */
            $this->_set_form_validation_rules();
            if ($this->form_validation->run()) {
        
                /* 登录成功 */
                $u_name = $this->input->post('u_name');
                $user = $this->user_model->get_by_name($u_name);
                $this->session->set_userdata(array(
                    'uid' => $user['u_id'],
                    'uname' => $user['u_name'],
                    'urealname' => $user['u_real_name'],
                ));
                
                redirect('welcome', 'refresh');
            }
        }
        
        /* 显示登录页面 */
		$this->load->view('header', $data);
        $this->load->view('login', $data);
        $this->load->view('footer');
    }
    
    /**
     * 退出
     */
    public function logout() {
        $this->session->set_userdata('uid');

        $data = array(
            'title' => '捷立信档案管理系统 - 退出',
            'sucess_message' => '您已成功退出系统，欢迎再次使用。'
        );
        $this->load->view('header', $data);
        $this->load->view('footer');
    }
    
    public function error($message) {
        $data = array(
            'title' => '出错了！',
            'error_message' => urldecode($message)
        );
        $this->load->view('header', $data);
        $this->load->view('footer');
    }

    /**
     * 验证用户名和密码
     * @param type $u_password
     * @return boolean
     */
    public function password_check($u_password) {
        $u_name = $this->input->post('u_name');
        if ($u_name == '') {
            return true;
        }
        
        if (!$this->user_model->validate($u_name, $u_password)) {
            $this->form_validation->set_message('password_check', '用户名或密码输入错误.');
            return false;
        }
        
        $user = $this->user_model->get_by_name($u_name);
        $today = date('Y-m-d');
        if ($user['u_first_date'] > $today || $user['u_last_date'] < $today) {
            $this->form_validation->set_message('password_check', '该用户已过期.');
            return false;
        }
        return true;
    }
    
    public function php_info() {
        phpinfo();
    }

    private function _set_form_validation_rules() {
        $fv = $this->form_validation;
        
        $fv->set_rules('u_name', '用户名', 'trim|required');
        $fv->set_rules('u_password', '密码', 'required|callback_password_check');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */